/**
 * 
 */
package fr.fermeManager.application.views.models;

import java.util.GregorianCalendar;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import fr.fermeManager.library.model.beans.Ferme;

/**
 * @author Mo
 *
 */
public class ModelGridFermes extends AbstractTableModel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@SuppressWarnings("rawtypes")
	private final Class[] columnClasses = {Integer.class,Object.class,Object.class,String.class,String.class,GregorianCalendar.class};
	
	private final String[] entetes = {"Id", "Actif", "Principale", "Libelle", "Description" , "Date"};
	
	private List<Ferme> listeFermes;
	
	
	public ModelGridFermes(List<Ferme> listeFermesParam) {
		listeFermes = listeFermesParam;
	}

	public int getRowCount() {
		
		return listeFermes.size();
	}

	public int getColumnCount() {
		return entetes.length;
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		switch (columnIndex) {
		case 0:
			return listeFermes.get(rowIndex).getId();
		case 1:
			return listeFermes.get(rowIndex).isActif();
		case 2:
			return listeFermes.get(rowIndex).isPrincipale();
		case 3:
			return listeFermes.get(rowIndex).getLibelle();
		case 4:
			return listeFermes.get(rowIndex).getDescription();
		case 5:
			return listeFermes.get(rowIndex).getDateCreation();
		default:
			return null;
		}
		
	}
	
	public void ajouterTransaction(Ferme newFerme){
		this.listeFermes.add(newFerme);
		fireTableRowsInserted(listeFermes.size()-1, listeFermes.size()-1);
	}
	
	public void supprimerTransaction(int idFermeParam) {
		Ferme fermeTrouvee = null;
		for (Ferme fermeTmp : this.listeFermes) {
			if(fermeTmp.getId() == idFermeParam){
				fermeTrouvee = fermeTmp;
				break;
			}
		}
		int indiceElem = this.listeFermes.indexOf(fermeTrouvee);
		fireTableRowsDeleted(indiceElem, indiceElem);
	}

	@Override
	public String getColumnName(int column) {
		return entetes[column];
	}
	
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}
	
	public Ferme getElementByIndex(int index){
		return listeFermes.get(index);
	}
	
	/* (non-Javadoc)
	 * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
	 */
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return columnClasses[columnIndex];
	}

}
